using System;
using System.Collections.Generic;
using System.Xml;
using System.Collections.ObjectModel;

namespace planning
{
	public class TD : Enseignement
	{
		// ----- ATTRIBUTS -----


		// ----- CONSTRUCTEURS -----
		public TD()
		{
			// Start of user code for default constructor
			// End of user code for default constructor
		}

		// ----- ACCESSEURS -----



		private static Dictionary<string, TD> references = new Dictionary<string, TD>();

		public static new TD fromXml(XmlNode node)
		{
			TD td;
			XmlNode refAttribute = node.SelectSingleNode("@ref");
			XmlNode idAttribute = node.SelectSingleNode("@id");

			if(refAttribute != null) {
				if(references.ContainsKey(refAttribute.Value)) {
					return references[refAttribute.Value];
				} else {
					td = new TD();
					references.Add(refAttribute.Value, td);
					return td;
				}
			}

			if(idAttribute != null && references.ContainsKey(idAttribute.Value)) {
				td = references[idAttribute.Value];
			} else {
				td = new TD();

				if(idAttribute != null) {
					references.Add(idAttribute.Value, td);
				}
			}

			if(node.SelectSingleNode("@groupe") != null)
				td.Groupe = (NumeroGroupe) Enum.Parse(typeof(NumeroGroupe), node.SelectSingleNode("@groupe").Value);

			foreach (XmlNode subElement in node.SelectNodes("horaires/*"))
			{
				Horaire subObject = Horaire.fromXml(subElement);
				td.Horaires.Add(subObject);
			}

			if(node.SelectSingleNode("./matiere/*") != null)
				td.Matiere = Matiere.fromXml(node.SelectSingleNode("./matiere/*"));

			if(node.SelectSingleNode("./enseignant/*") != null)
				td.Enseignant = Enseignant.fromXml(node.SelectSingleNode("./enseignant/*"));

			if(node.SelectSingleNode("./salle/*") != null)
				td.Salle = Salle.fromXml(node.SelectSingleNode("./salle/*"));

			

			return td;

		}
	}
}
